package graph.drawingshapes.shapes
{
	import graph.drawingshapes.RedrawEvent;
	import graph.geometricshapes.GeometricPolygon;
	import graph.geometricshapes.GeometricShapeFactory;
	
	import mx.collections.ArrayCollection;
	
	public class PolygonShape extends FilledDrawingShape
	{
		public static const NAME:String = "PolygonShape";
		public override function get getName():String
		{
			return NAME;
		}
		
		[Bindable]
		private var _lines:ArrayCollection = new ArrayCollection();
		[Bindable]
		public function get lines():ArrayCollection
		{
			return this._lines;
		}
		public function set lines(value:ArrayCollection):void
		{
			this._lines = value;
		}
		
		[Bindable]
		public override function get width():Number
		{
			var positiveSum:Number = 0;
			var sum:Number = 0;
			for each(var line:LineShape in this.lines)
			{
				if (line.width >= 0)
				{
					positiveSum += line.width;
				}
				sum += line.width;
			}
			var result:Number = 0;
			if (Math.abs(sum) > 0)
			{
				result = positiveSum + Math.abs(sum);
			}
			else
			{
				result = positiveSum;
			}
			return result;
		}
		public override function set width(value:Number):void
		{
			if (this.width == 0 || value == 0) return;
			var coef:Number = Math.abs(value / this.width);
			if (coef != 0)
			{
				for each(var line:LineShape in this.lines)
				{
					line.width *= coef;
					line.width *= value/Math.abs(value);
				}
			}
			super.width = value;
			dispatchEvent(new RedrawEvent());
		}
		
		[Bindable]
		public override function get height():Number
		{
			var positiveSum:Number = 0;
			var sum:Number = 0;
			for each(var line:LineShape in this.lines)
			{
				if (line.height >= 0)
				{
					positiveSum += line.height;
				}
				sum += line.height;
			}
			var result:Number = 0;
			if (Math.abs(sum) > 0)
			{
				result = positiveSum + Math.abs(sum);
			}
			else
			{
				result = positiveSum;
			}
			return result;
		}
		public override function set height(value:Number):void
		{
			if (this.height == 0 || value==0) return;
			var coef:Number = Math.abs(value / this.height);
			if (coef != 0)
			{
				for each(var line:LineShape in this.lines)
				{
					line.height *= coef;
					line.height *= value / Math.abs(value);
				}
			}
			super.height = value;
			dispatchEvent(new RedrawEvent());
		}
		
		public function PolygonShape(x:Number=DefaultX, y:Number=DefaultY, width:Number=DefaultWidth, height:Number=DefaultHeight)
		{
			super(x, y);
			this.geometricShape = GeometricShapeFactory.createShape(GeometricPolygon.NAME, width, height);
			dispatchEvent(new RedrawEvent()); 
		}
		
		
		protected override function initDrawing():void
		{
			super.initDrawing();
			removeAll();
		}
		
		protected function removeAll():void
		{
		 	while (this.numChildren > 0)
			{
				this.removeChildAt(0);
			}
		}
		
		protected override function draw():void
		{
			super.draw();
			var currX:Number = 0;
			var currY:Number = 0;
			for each(var line:LineShape in this.lines)
			{
				line.x = currX;
				line.y = currY;
				this.addChild(line);
				currX += line.width;
				currY += line.height;
			}
		}		
	}
}